﻿using Coldairarrow.Business;
using Coldairarrow.Business.PB;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using LinqKit;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.PB
{
    [Route("/PB/[controller]/[action]")]
    public class PB_StationWorkStationController : BaseApiController
    {
        #region DI

        public PB_StationWorkStationController(IPB_StationWorkStationBusiness pB_StationWorkStationBus)
        {
            _pB_StationWorkStationBus = pB_StationWorkStationBus;
        }

        IPB_StationWorkStationBusiness _pB_StationWorkStationBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<PB_StationWorkStation>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _pB_StationWorkStationBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<PB_StationWorkStation> GetTheData(IdInputDTO input)
        {
            return await _pB_StationWorkStationBus.GetTheDataAsync(input.id);
        }

        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(PB_StationWorkStationDto data)
        {
            //先删除原有的Stationworkstationid的所有数据
            var where = LinqHelper.True<PB_StationWorkStation>();
            if (!data.StationId.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_StationWorkStation, bool>(
                        ParsingConfig.Default, false, $@"StationId.Equals(@0)", data.WorkStationId);
                where = where.And(newWhere);
            }
            await _pB_StationWorkStationBus.DeleteOtherIdDataAsync(where);
            //插入新的

            if (data.Id.IsNullOrEmpty())
            {

                foreach(var item in data.StationId){
                    InitEntity(data);
                    PB_StationWorkStation station = new PB_StationWorkStation();
                    station.Id = data.Id;
                    station.WorkStationid = data.WorkStationId;
                    station.Stationid = item.ToString();
                    await _pB_StationWorkStationBus.AddDataAsync(station);
                }
               
            }
            else
            {
                //修改
                foreach (var item in data.StationId) {
                    PB_StationWorkStation station = new PB_StationWorkStation();
                    station.Id = data.Id;
                    station.WorkStationid = data.WorkStationId;
                    station.Stationid = item.ToString();
                    await _pB_StationWorkStationBus.UpdateDataAsync(station);
                }
            }
        }
        /// <summary>
        /// 根据工作站查询 PB_StationWorkStation
        /// </summary>
        [HttpPost]
        public async Task<List<PB_StationWorkDTO>> GetDataListByArea(string WorkStationid)
        {
            var res = await _pB_StationWorkStationBus.GetDataListAsync(WorkStationid);
            return res;
        }
        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _pB_StationWorkStationBus.DeleteDataAsync(ids);
        }

        #endregion
    }
}